﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace DataAccess
{
    public class MyQueryDA: DBConnection
    {
        public MyQueryDA()
        {
            sqlCommand.Connection = sqlConnection;
        }

        public string[] doQuery(string query)
        {
            SqlDataAdapter adapter;
            DataTable dt = new DataTable("My Table");
            sqlCommand.CommandText = query;
            try
            {
                open();
                adapter = new SqlDataAdapter(sqlCommand);
                adapter.Fill(dt);
                if (dt != null)
                {
                    close();
                    return UtilsDA.convertDataTableToStringArray(dt);
                }
                return null;
            }
            catch(SqlException s)
            {
                Console.WriteLine(s.Message);
                return null;
            }
            finally
            {
                if (sqlDataReader != null)
                    sqlDataReader.Close();
                close();

            }
        }
        /// <summary>
        /// Excute a custom non-query
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public bool doNonQuery(string query)
        {
            sqlCommand.CommandText = query;
            try
            {
                open();
                sqlCommand.ExecuteNonQuery();
                close();
                return true;
            }
            catch (SqlException s)
            {
                //log here
                Console.WriteLine(s.Message);
                return false;
            }
        }
    }
}
